﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using ROYcms.DB;
namespace ROYcms.Sys.DAL
{
	/// <summary>
	/// 数据访问类:ROYcms_Transaction
	/// </summary>
	public partial class ROYcms_Transaction
	{
		public ROYcms_Transaction()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("Id", "ROYcms_Transaction"); 
		}


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from ROYcms_Transaction");
			strSql.Append(" where Id="+Id+" ");
			return DbHelperSQL.Exists(strSql.ToString());
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(ROYcms.Sys.Model.ROYcms_Transaction model)
		{
			StringBuilder strSql=new StringBuilder();
			StringBuilder strSql1=new StringBuilder();
			StringBuilder strSql2=new StringBuilder();
		
				strSql1.Append("UserId,");
				strSql2.Append(""+model.UserId+",");
			
			
				strSql1.Append("TradeType,");
				strSql2.Append(""+model.TradeType+",");
			
		
				strSql1.Append("Price,");
				strSql2.Append(""+model.Price+",");
		
			
				strSql1.Append("AccountBalance,");
				strSql2.Append(""+model.AccountBalance+",");
			
			if (model.PayentMethord != null)
			{
				strSql1.Append("PayentMethord,");
				strSql2.Append("'"+model.PayentMethord+"',");
			}
			if (model.TradeExplaining != null)
			{
				strSql1.Append("TradeExplaining,");
				strSql2.Append("'"+model.TradeExplaining+"',");
			}
			if (model.PaymentLogo != null)
			{
				strSql1.Append("PaymentLogo,");
				strSql2.Append("'"+model.PaymentLogo+"',");
			}
			if (model.Remark != null)
			{
				strSql1.Append("Remark,");
				strSql2.Append("'"+model.Remark+"',");
			}
			if (model.CreateTime != null)
			{
				strSql1.Append("CreateTime,");
				strSql2.Append("'"+model.CreateTime+"',");
			}
			strSql.Append("insert into ROYcms_Transaction(");
			strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
			strSql.Append(")");
			strSql.Append(";select @@IDENTITY");
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(ROYcms.Sys.Model.ROYcms_Transaction model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update ROYcms_Transaction set ");
		
				strSql.Append("UserId="+model.UserId+",");
		
			
				strSql.Append("TradeType="+model.TradeType+",");
			
		
				strSql.Append("Price="+model.Price+",");
			
			
				strSql.Append("AccountBalance="+model.AccountBalance+",");
			
			if (model.PayentMethord != null)
			{
				strSql.Append("PayentMethord='"+model.PayentMethord+"',");
			}
			else
			{
				strSql.Append("PayentMethord= null ,");
			}
			if (model.TradeExplaining != null)
			{
				strSql.Append("TradeExplaining='"+model.TradeExplaining+"',");
			}
			else
			{
				strSql.Append("TradeExplaining= null ,");
			}
			if (model.PaymentLogo != null)
			{
				strSql.Append("PaymentLogo='"+model.PaymentLogo+"',");
			}
			else
			{
				strSql.Append("PaymentLogo= null ,");
			}
			if (model.Remark != null)
			{
				strSql.Append("Remark='"+model.Remark+"',");
			}
			else
			{
				strSql.Append("Remark= null ,");
			}
			if (model.CreateTime != null)
			{
				strSql.Append("CreateTime='"+model.CreateTime+"',");
			}
			else
			{
				strSql.Append("CreateTime= null ,");
			}
			int n = strSql.ToString().LastIndexOf(",");
			strSql.Remove(n, 1);
			strSql.Append(" where Id="+ model.Id+"");
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from ROYcms_Transaction ");
			strSql.Append(" where Id="+Id+"" );
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from ROYcms_Transaction ");
			strSql.Append(" where Id in ("+Idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ROYcms.Sys.Model.ROYcms_Transaction GetModel(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1  ");
			strSql.Append(" Id,UserId,TradeType,Price,AccountBalance,PayentMethord,TradeExplaining,PaymentLogo,Remark,CreateTime ");
			strSql.Append(" from ROYcms_Transaction ");
			strSql.Append(" where Id="+Id+"" );
			ROYcms.Sys.Model.ROYcms_Transaction model=new ROYcms.Sys.Model.ROYcms_Transaction();
			DataSet ds=DbHelperSQL.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["Id"]!=null && ds.Tables[0].Rows[0]["Id"].ToString()!="")
				{
					model.Id=int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["UserId"]!=null && ds.Tables[0].Rows[0]["UserId"].ToString()!="")
				{
					model.UserId=int.Parse(ds.Tables[0].Rows[0]["UserId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["TradeType"]!=null && ds.Tables[0].Rows[0]["TradeType"].ToString()!="")
				{
					model.TradeType=int.Parse(ds.Tables[0].Rows[0]["TradeType"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Price"]!=null && ds.Tables[0].Rows[0]["Price"].ToString()!="")
				{
					model.Price=int.Parse(ds.Tables[0].Rows[0]["Price"].ToString());
				}
				if(ds.Tables[0].Rows[0]["AccountBalance"]!=null && ds.Tables[0].Rows[0]["AccountBalance"].ToString()!="")
				{
					model.AccountBalance=int.Parse(ds.Tables[0].Rows[0]["AccountBalance"].ToString());
				}
				if(ds.Tables[0].Rows[0]["PayentMethord"]!=null && ds.Tables[0].Rows[0]["PayentMethord"].ToString()!="")
				{
					model.PayentMethord=ds.Tables[0].Rows[0]["PayentMethord"].ToString();
				}
				if(ds.Tables[0].Rows[0]["TradeExplaining"]!=null && ds.Tables[0].Rows[0]["TradeExplaining"].ToString()!="")
				{
					model.TradeExplaining=ds.Tables[0].Rows[0]["TradeExplaining"].ToString();
				}
				if(ds.Tables[0].Rows[0]["PaymentLogo"]!=null && ds.Tables[0].Rows[0]["PaymentLogo"].ToString()!="")
				{
					model.PaymentLogo=ds.Tables[0].Rows[0]["PaymentLogo"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Remark"]!=null && ds.Tables[0].Rows[0]["Remark"].ToString()!="")
				{
					model.Remark=ds.Tables[0].Rows[0]["Remark"].ToString();
				}
				if(ds.Tables[0].Rows[0]["CreateTime"]!=null && ds.Tables[0].Rows[0]["CreateTime"].ToString()!="")
				{
					model.CreateTime=DateTime.Parse(ds.Tables[0].Rows[0]["CreateTime"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select Id,UserId,TradeType,Price,AccountBalance,PayentMethord,TradeExplaining,PaymentLogo,Remark,CreateTime ");
			strSql.Append(" FROM ROYcms_Transaction ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" Id,UserId,TradeType,Price,AccountBalance,PayentMethord,TradeExplaining,PaymentLogo,Remark,CreateTime ");
			strSql.Append(" FROM ROYcms_Transaction ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM ROYcms_Transaction ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.Id desc");
			}
			strSql.Append(")AS Row, T.*  from ROYcms_Transaction T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		*/

		#endregion  Method
	}
}

